<?php
class Admin_PaymentTypeController extends Apps_Controller_Action_Admin {
	public function indexAction() {
    	$this->actResolve('act_delete',
    			'delete', 'payment-type', 'admin', array('id' => $this->_getParam('item_checker')),
    			true);

		$tbPaymentType = new M_DbTable_PaymentType();
		$grid = new Apps_Plugin_Grid('PaymentType');
		$grid->setData($tbPaymentType->getList())
			 ->setTableCaption('Payment Type')
			 ->setTableColumnFormat(array(
                array(
                    'title'     => 'TT',
                    'dataField' => '_rank_',
                    'width'     => '3%'
                ),
                array(
                    'title'     => 'Title',
                    'dataField'	=> 'title'
                ),
                array(
                    'title'     => 'Description',
                    'dataField'	=> 'description'
                ),
                array(
                    'title'     => 'Created Date',
                    'dataField'	=> 'created_date'
                ),
			    array(
			    	'title'   	=> 'Edit',
				   	'html'      => array('<a href="/admin/payment-type/edit?id=%s">
				   						  <i class="icon-edit" title="Sửa" ></i></a>', array('id')),
	 				'width'	  	=> '2%'
				),
		 		array(
		 			'title'     => 'Delete',
	 				'html'      => array('<a href="/admin/payment-type/delete?id=%s">
				 						 <i class="icon-remove must-confirm" title="Xóa" data-must-confirm-msg="Xóa TT Kiểu thanh toán ?" ></i></a>',
				 						 array('id')),
	 				'width'	  	=> '2%'
		 		)
			));

        $this->view->grid = $grid;
    }

    public function addAction() {
        $rq   = $this->_request;
        $form = new Admin_Form_AddPaymentType();
    	$tbPaymentType = new M_DbTable_PaymentType();
        if($rq->isPost()) {
            if($form->isValid($rq->getParams())) {
            	$r = $tbPaymentType->safelyInsert($form->getValues());
                $this->_flashMessenger->addMessage(
                    $r
                    ? self::$msgAddSuccess
                    : self::$msgAddFailed);
                return $this->goToActionIndex();
            }
        }

        $this->view->form = $form;
    }

	public function editAction() {
		$rq = $this->_request;
        $id = $rq->getParam('id');
    	$tbPaymentType = new M_DbTable_PaymentType();

        if(!$id || !($r = $tbPaymentType->fetchRow(array('id=?' => $id))->toArray())) {
            $this->_flashMessenger->addMessage(self::$msgNoRecordFound);
            return $this->goToActionIndex();
        }

        $form = new Admin_Form_AddPaymentType();

        if($rq->isPost()) {
            if($form->isValid($rq->getParams())) {
            	$r = $tbPaymentType->safelyUpdate($form->getValues(), array('id=?' => $id));
                $this->_flashMessenger->addMessage(
                	$r
                    ? self::$msgUpdateSuccess
                    : self::$msgUpdateFailed);
                return $this->goToActionIndex();
            }
        } else {
            $form->populate($r);
        }
		$form->setLegend('Edit Payment Type');
        $this->view->form = $form;
    }

    public function deleteAction() {
    	$this->disableLayout(true, true);
    	$rq = $this->_request;
    	$paymentTypeIds = $rq->getParam('id');
    	if(!$paymentTypeIds) {
    		$this->goToActionIndex();
    	}

    	$tbPaymentType = new M_DbTable_PaymentType();
    	if(is_array($paymentTypeIds)) {
    		foreach ($paymentTypeIds as $paymentTypeId) {
    			$r = $tbPaymentType->safelyDelete(array('id = ?' => $paymentTypeId));
    		}
    		$this->goToActionIndex();
    	} elseif (is_numeric($paymentTypeIds)) {
    		$r = $tbPaymentType->safelyDelete(array('id = ?' => $paymentTypeIds));
    		$this->_flashMessenger->addMessage($r ? self::$msgDeleteSuccess : self::$msgDeleteError);
    		$this->goToActionIndex();
    	} else {
    		$this->goToActionIndex();
    	}
    }
}